/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.atjava.jcrweb.JcrWebException;
import com.atjava.jcrweb.info.LinkInfo;
import com.atjava.jcrweb.manager.LinkManager;
import com.atjava.jcrweb.manager.PackingToFlexHelper;
/**
 * 
 * 
 * @author biggie
 * @version $Revision: 1.5 $ $Date: 2008/07/17 02:07:39 $
 */
public class LoadMyHomeDataFlexServlet extends FlexBaseServlet {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String process(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata)
			throws IOException, ServletException , JcrWebException , Exception {

		String type= getData(inputdata, "type");
		if(type.equals("loadLink")){
			return loadMyHomeData(request,response,inputdata);
		}
		return null;
	}
	private String loadMyHomeData(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata)
	throws IOException, ServletException , JcrWebException , Exception {
		
		LinkManager linkMgr = LinkManager.getInstance();
		List listLink =  linkMgr.getLinksByUserCode(getData(inputdata, "userCode"));
		return toXmldata(listLink);
	}
	private String toXmldata(List listLink){
		PackingToFlexHelper helper = new PackingToFlexHelper();
		StringBuffer sb = new StringBuffer();
		if((listLink != null && listLink.size() > 0)){
			helper.setResultSize(String.valueOf(listLink.size()));
			for(int i=0;i<listLink.size();i++){
				LinkInfo info = (LinkInfo)listLink.get(i);
				sb.append("<"+(i+1)+">");
				sb.append("<data>");
				sb.append(info.toXmlString());
				sb.append("</data>");
				sb.append("</"+(i+1)+">");
			}
		}
		helper.setIsError(false);
		helper.setResultData(sb.toString());
		return helper.toString();
	}
}
